﻿using Microsoft.EntityFrameworkCore;
using XLZF.MicroServices.IRepository;
using XLZF.MicroServices.Models;

namespace XLZF.MicroServices.Repository;

public class ClothingClassDetailRepository : IClothingClassDetailRepository
{
    private static ModelMigration.XLZFContext context = new ModelMigration.XLZFContext();

    private IRepository<Clothing_Class_Detail> _repository = new Repository<Clothing_Class_Detail>(context);

    public async Task<int> Add(Clothing_Class_Detail model)
    {
        return await _repository.Add(model);
    }

    public int Del(Clothing_Class_Detail model)
    {
        return _repository.Remove(model);
    }

    public int Edit(Clothing_Class_Detail model)
    {
        return _repository.Update(model);
    }

    public Clothing_Class_Detail Get(string ID)
    {
        return _repository.GetModel(p => p.ID == ID);
    }

    public List<Clothing_Class_Detail> GetLists()
    {
        return _repository.GetAll().ToList();
    }

    public Page<Clothing_Class_Detail> GetPageData(string name, bool isAsc, int pageIndex, int pageSize)
    {
        Page<Clothing_Class_Detail>? data = new Page<Clothing_Class_Detail>() { PageIndex = pageIndex, PageSize = pageSize };

        int skip = (pageIndex - 1) * pageSize;//skip 跳过多少条数据 take 取多少数据

        var lq = _repository.GetAll();

        if (!string.IsNullOrEmpty(name))
        {
            lq = lq.Where(p => p.DetailName.Contains(name) || p.Clothing_Class_Dict.ClassName.Contains(name));
        }

        if (isAsc)
        {
            lq = lq.OrderBy(e => e.CreateDate);
        }
        else
        {
            lq = lq.OrderByDescending(e => e.CreateDate);
        }

        data.LsList = lq.Include(e => e.Clothing_Class_Dict).Skip(skip).Take(pageSize).AsQueryable().ToList();

        data.TotalRows = lq.Count();

        data.TotalPages = data.TotalRows / pageSize;

        return data;
    }
}
